package com.tin.restfinder;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;



public class nazwa extends ListActivity  {
	private EditText filterText = null;
    private RestAdapter m_adapter;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    setContentView(R.layout.nazwa);
	
	    filterText = (EditText) findViewById(R.id.search_box);
	    filterText.addTextChangedListener(filterTextWatcher);
	    
        m_adapter=new RestAdapter(nazwa.this, R.layout.rest_row, Data.conf.getList());
        setListAdapter(m_adapter);	
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);		
		Data.SetRestDetailsByName(m_adapter.getItem(position).getName());
		
		Intent Tab = new Intent(nazwa.this, RestDetails.class);
		startActivity(Tab);
	}
	protected void onResume(){
		super.onResume();
		m_adapter=new RestAdapter(nazwa.this, R.layout.rest_row, Data.conf.getList());
		setListAdapter(m_adapter);
		m_adapter.notifyDataSetChanged();
	}
	private class RestAdapter extends ArrayAdapter<Restaurant> implements Filterable {

        private PTypeFilter filter;
        public ArrayList<Restaurant> subItems;
        public ArrayList<Restaurant> allItems;

        public RestAdapter(Context context, int textViewResourceId, ArrayList<Restaurant> items) {
                super(context, textViewResourceId, items);
                this.subItems = items;
                this.allItems=this.subItems;
        }
        @Override
        public int getCount()
        {
			return subItems.size();
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.rest_row, null);
                }
                Restaurant o = subItems.get(position);
                if (o != null) {
                        TextView name = (TextView) v.findViewById(R.id.rest_name_list);
                        TextView tags = (TextView) v.findViewById(R.id.rest_tags_list);
                        TextView address = (TextView) v.findViewById(R.id.rest_address);
                        final RatingBar ratingbar = (RatingBar) v.findViewById(R.id.rest_rate_list);
                        final ImageView rest_logo = (ImageView) v.findViewById(R.id.rest_logo);
                        if (name != null) {
                              name.setText(o.getName());                            }
                        if (name != null) {
                            address.setText(o.getStreet()+", "+o.getCity());                            }
                        if(tags != null){
                              tags.setText("Tagi: ");
                              for(int i=0;i<o.getTags().size();i++)
                              {
                            	  tags.append(o.getTags().get(i));
                            	  if(i+1!=o.getTags().size())
                            		  tags.append(", ");
                              }
                        }
                        if(ratingbar != null) {
                        	ratingbar.setRating((float) o.getRating());
                        }
                        if (rest_logo != null) {
                            rest_logo.setImageBitmap(o.getLogo());
                        }
                }
                return v;
        }
        @Override
        public Filter getFilter() {
            if (filter == null){
              filter  = new PTypeFilter();
            }
            return filter;
          }
        private class PTypeFilter extends Filter{

            @SuppressWarnings("unchecked")
			@Override
            protected void publishResults(CharSequence prefix,
                                          FilterResults results) {
              // NOTE: this function is *always* called from the UI thread.
               subItems =  (ArrayList<Restaurant>)results.values;

                notifyDataSetChanged();
            }

	        protected FilterResults performFiltering(CharSequence prefix) {
	              // NOTE: this function is *always* called from a background thread, and
	              // not the UI thread. 
	
	              FilterResults results = new FilterResults();
	              ArrayList<Restaurant> i = new ArrayList<Restaurant>();
	
	              if (prefix!= null && prefix.toString().length() > 0) {
	
	                  for (int index = 0; index < allItems.size(); index++) {
	                      Restaurant si = allItems.get(index);
	                      if(si.getName().toLowerCase().contains(prefix.toString())){
	                        i.add(si);  
	                      }
	                  }
	                  results.values = i;
	                  results.count = i.size();                   
	              }
	              else{
	                  synchronized (allItems){
	                      results.values = allItems;
	                      results.count = allItems.size();
	                  }
	              }
	
	              return results;
	        }
      }     
        
	}
	private TextWatcher filterTextWatcher = new TextWatcher() {
	
	    public void afterTextChanged(Editable s) {
	    }
	
	    public void beforeTextChanged(CharSequence s, int start, int count,
	            int after) {
	    }
	
	    public void onTextChanged(CharSequence s, int start, int before,
	            int count) {
	    	m_adapter.getFilter().filter(s);
	    }
	
	};
	
	

@Override
protected void onDestroy() {
    super.onDestroy();
    filterText.removeTextChangedListener(filterTextWatcher);
};

}